﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Crawler.Framework;
using Crawler.Model.SystemDefault;
using Newtonsoft.Json;

namespace Crawler.App.Manager.Controllers
{
    public class ProxyIPController : Controller
    {
        private readonly MyDbContext _context;

        public ProxyIPController(MyDbContext context)
        {
            _context = context;
        }

        // GET: ProxyIPModels
        // GET: CollectInfoConfigModels
        public async Task<IActionResult> Index()
        {
            return View();
        }

        public async Task<IActionResult> List(int page, int limit)
        {
            var count = await _context.ProxyIPs.CountAsync();
            var data = await _context.ProxyIPs.Skip((page - 1) * limit).Take(limit).ToListAsync();
            return Json(new { code = 0, msg = "", count, data });
        }

        // GET: ProxyIPModels/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return Json(new { code = 1, msg = "id为空" });
            }

            var model = await _context.ProxyIPs.FindAsync(id);
            if (model == null)
            {
                return Json(new { code = 1, msg = "找不到指定对象" });
            }
            return Json(new { code = 0, msg = "", data = model }); ;
        }

        // GET: ProxyIPModels/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: ProxyIPModels/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("IPAddress,Port,AddressStatus,ID")] ProxyIPModel proxyIPModel)
        {
            if (ModelState.IsValid)
            {
                _context.Add(proxyIPModel);
                await _context.SaveChangesAsync();
                return Json(new { code = 0, msg = "" });
            }
            return Json(new { code = 1, msg = $"数据验证失败:{JsonConvert.SerializeObject(ModelState.Values)}" });
        }

        // GET: ProxyIPModels/Edit/5
        public async Task<IActionResult> Edit()
        {
            return View();
        }

        // POST: ProxyIPModels/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("IPAddress,Port,AddressStatus,ID")] ProxyIPModel proxyIPModel)
        {
            if (id != proxyIPModel.ID)
            {
                return Json(new { code = 1, msg = "找不到指定对象" });
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(proxyIPModel);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProxyIPModelExists(proxyIPModel.ID))
                    {
                        return Json(new { code = 1, msg = "找不到指定对象" });
                    }
                    else
                    {
                        throw;
                    }
                }
                return Json(new { code = 0, msg = "" });
            }
            return Json(new { code = 1, msg = $"数据验证失败:{JsonConvert.SerializeObject(ModelState.Values)}" });
        }

        // GET: ProxyIPModels/Delete/5
        public async Task<IActionResult> Delete(int[] id)
        {
            if (id == null)
            {
                return Json(new { code = 1, msg = "id为空" });
            }
            var models = await _context.ProxyIPs.Where(c => id.Contains(c.ID)).ToListAsync();

            models.ForEach(c =>
            {
                _context.ProxyIPs.Remove(c);
            });
            await _context.SaveChangesAsync();
            return Json(new { code = 0, msg = "", data = models });
        }

        private bool ProxyIPModelExists(int id)
        {
            return _context.ProxyIPs.Any(e => e.ID == id);
        }
    }
}
